package lego.jbv;

import lego.BT.BTController;
import lego.Moving.*;
import lego.animal.*;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.util.Delay;

public class Prey {	
	public final static int NUMBER_OF_PREY = 5;
	public static IAnimal preyArray[] = new IAnimal[NUMBER_OF_PREY];
	public static int prey;
	public static SoundController soundController;

	public static void main(String[] args) {
		//Add all prey to use to an array
		preyArray[0] = new Wildebeest();
		preyArray[1] = new Zebra();
		preyArray[2] = new Gazelle();
		preyArray[3] = new Human();
		preyArray[4] = new Rabbit();
		prey = selectPrey()-1;
		
		soundController = new SoundController();
		soundController.start();
		
		BTController BTThread = new BTController(preyArray[prey], soundController);
		BTThread.start();

		Hunger hunger = new Hunger(preyArray[prey], soundController);
		
		// Add create and add all behavior action to a list
		Behavior wander = new Wander(preyArray[prey], soundController);
		Behavior eating = new Eat(hunger);
		Behavior escape = new Escape(BTThread);
		Behavior avoid = new Avoid();
		Behavior stop = new Stop(BTThread);
		Behavior[] behaviorList = {avoid, escape, eating,  wander, stop};
		Arbitrator arbitrator = new Arbitrator(behaviorList);
		
		Display.init(preyArray[prey]);

		hunger.start();
		arbitrator.start();
	}
	
	public static int selectPrey(){
		//int chose = 1;
		LCD.drawString("Choose a prey:", 0, 0);
		for(int i = 0; i < NUMBER_OF_PREY; i++)
		{
			LCD.drawString(preyArray[i].getName(), 2, i+1);
		}
		LCD.drawChar('>', 0, 1);
		
		int chose = 1;
		while(Button.ENTER.isUp())
		{
			if(Button.LEFT.isDown())
			{
				LCD.drawChar(' ', 0, chose);
				chose = decreasChose(chose);
				LCD.drawChar('>', 0, chose);
				Delay.msDelay(300);
			}
			
			else if(Button.RIGHT.isDown())
			{
				LCD.drawChar(' ', 0, chose);
				chose = increasChose(chose);
				LCD.drawChar('>', 0, chose);
				Delay.msDelay(300);
			}
			else if(Button.ESCAPE.isDown())
			{
				//LCD.drawString("Extiting", 0, 7);
				System.exit(1);
			}
			
			Delay.msDelay(100);
			//while(Button.RIGHT.isDown() | Button.LEFT.isDown()){}
		}
		return chose;
	}
	
	private static int decreasChose(int c)
	{
		if(c == 1)
			c = NUMBER_OF_PREY;
		else
			c--;
		return c;
	}
	
	private static int increasChose(int c)
	{
		if(c == NUMBER_OF_PREY)
			c = 1;
		else
			c++;
		return c;
	}
}

